package com.hzvdvmc.autosale.setting.ui;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.setting.adapter.SimpleTextSpinnerAdatper;
import com.hzvdvmc.autosale.setting.bean.TuneSetBean;
import com.hzvdvmc.autosale.util.GsonUtils;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.SPUtils;
import com.hzvdvmc.autosale.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 货道设置
 */
public class TuneSetActivity extends ActionbarActivity {

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.spinner)
    Spinner spinner;
    SpinnerAdapter mAdapter;
    private List<String> spinnerDataList = new ArrayList<>();
    private List<TuneSetFragment> fragmentList = new ArrayList<>();
    private PagerAdapter fragmentPagerAdapter;
    private String tuneSetJson;
    private TuneSetBean mDataBean = new TuneSetBean();

    private static final int CAB_MAX = 4;
    private static final int ROW_MAX = 6;
    private static final int COLUMN_MAX = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tune_set);
        ButterKnife.bind(this);
        setCustomeActiorBar("货道设置");
        setRightFuncBtn("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String json = GsonUtils.GsonString(mDataBean);
                LogUtil.i("将要保存的数据:" + json);
                SPUtils.getInstance().put(SpKey.TUNE_SET_JSON, json);
                ToastUtils.showShort("保存成功");
            }
        });
        getLocalData();
        initSpinnerDataList();
        initSpinner();
        initTabLayout();
    }

    private void initTabLayout() {
        if (mDataBean != null) {
            initFragmentList();
        }
    }

    private void getLocalData() {
        tuneSetJson = SPUtils.getInstance().getString(SpKey.TUNE_SET_JSON);
        LogUtil.i("上次保存的货道数据：" + tuneSetJson);
        if (!TextUtils.isEmpty(tuneSetJson)) {
            mDataBean = GsonUtils.GsonToBean(tuneSetJson, TuneSetBean.class);
        }
    }

    private void initSpinnerDataList() {
        for (int i = 1; i <= CAB_MAX; i++) {
            spinnerDataList.add(i + "");
        }

    }

    private void initSpinner() {
        mAdapter = new SimpleTextSpinnerAdatper(spinnerDataList);
        spinner.setPrompt("请选择总柜数");
        spinner.setAdapter(mAdapter);
        if (mDataBean != null) {
            spinner.setSelection(mDataBean.getDataList().size() - 1);
        }
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                refreshTabLayout(Integer.parseInt(spinnerDataList.get(position)));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void refreshTabLayout(int count) {
        if (count == 1) {
            tabLayout.setVisibility(View.GONE);
        } else {
            tabLayout.setVisibility(View.VISIBLE);
        }

        refreshFragmentList(count);
    }

    private void initViewPager() {
        tabLayout.setupWithViewPager(viewPager);
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return (position + 1) + "号柜";
            }

            @Override
            public int getItemPosition(Object object) {
                return PagerAdapter.POSITION_NONE;
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
    }

    private void initFragmentList() {
        initViewPager();
        for (int i = 0; i < mDataBean.getDataList().size(); i++) {
            fragmentList.add(TuneSetFragment.newInstance(mDataBean.getDataList().get(i)));
        }
        fragmentPagerAdapter.notifyDataSetChanged();
    }

    private void refreshFragmentList(int count) {
        if (mDataBean.getDataList().size() == count) {
            return;
        }
        initViewPager();
        fragmentList.clear();
        mDataBean.getDataList().clear();

        for (int i = 0; i < count; i++) {
            TuneSetBean.Cabinet cabinet = new TuneSetBean.Cabinet();
            cabinet.getDataList().clear();
            mDataBean.getDataList().add(cabinet);
            fragmentList.add(TuneSetFragment.newInstance(cabinet));
        }
        fragmentPagerAdapter.notifyDataSetChanged();
    }
}
